package com.lsn.watermall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lsn.watermall.entity.Result;
import com.lsn.watermall.entity.UserBean;
import com.lsn.watermall.exception.BaseErrorInfo;
import com.lsn.watermall.exception.ErrorInfo;
import com.lsn.watermall.mapper.UserMapper;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserMapper userMapper;
    @GetMapping("/getUser")
    public Result getUser(@RequestParam int id){
        UserBean userBean=userMapper.selectById(id);
        System.out.println("userBean="+userBean);
        return Result.ok(userBean);
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody UserBean userBean){
        System.out.println("addUser userBean="+userBean);
        int rownum=userMapper.insert(userBean);
        if(rownum==0){
            return Result.error(ErrorInfo.ERR9999);
        }
        return Result.ok(userBean);
    }

//    @PostMapping("/updateUser")
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody UserBean userBean){
        System.out.println("updateUser userBean="+userBean);
        int rownum=userMapper.update(userBean,new UpdateWrapper<UserBean>().eq("id",userBean.getId()));
        if(rownum==0){
            return Result.error(ErrorInfo.ERR9999);
        }
        return Result.ok(userBean);
    }

    @GetMapping("/getUserList")
    public List<UserBean> getUserList(){
        return userMapper.selectList(new QueryWrapper<>());
    }

    @GetMapping("/delUser")
    public Result delUser(@RequestParam int id){
        System.out.println("delUser id="+id);
        int rownum= userMapper.deleteById(id);
        if(rownum==0){
            return Result.error(ErrorInfo.ERR9999);
        }
        return Result.ok();
    }
}
